package novel.mzx.com.activity;


import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;


import com.gyf.immersionbar.ImmersionBar;


import java.util.ArrayList;

import butterknife.BindView;
import novel.mzx.com.R;
import novel.mzx.com.base.BaseActivity;
import novel.mzx.com.fragment.HomeFragment;
import novel.mzx.com.fragment.MeFragment;
import novel.mzx.com.fragment.ShareFragment;
import novel.mzx.com.fragment.VideoFragment;

/**
 * 进入首页的逻辑编写：
 * */

public class MainActivity extends BaseActivity {

    @BindView(R.id.frameLayout)
    FrameLayout frameLayout;

    @BindView(R.id.main_radioGroup)
    RadioGroup mainRadioGroup;

    @BindView(R.id.rb_recommend)
    RadioButton rb_recommend;

    HomeFragment homeFragment;
    ShareFragment shareFragment;
    VideoFragment videoFragment;

    MeFragment meFragment;


    private ArrayList<String> lists = new ArrayList<>();
    @Override
    protected int getResourceId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        super.initView();

        rb_recommend.setChecked(true);

        showFragment(0);


    }




    // 初始化监听器
    @Override
    protected void initListener() {
        super.initListener();

        mainRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_recommend:
                        // 沉浸式管理导航栏、状态栏
                        ImmersionBar.with(MainActivity.this).keyboardEnable(false).statusBarDarkFont(true).init();
                        showFragment(0);
                        break;
                    case R.id.rb_02:
                        ImmersionBar.with(MainActivity.this).keyboardEnable(false).statusBarDarkFont(true).init();
                        showFragment(1);
                        break;
                    case R.id.rb_03:
                        ImmersionBar.with(MainActivity.this).keyboardEnable(false).statusBarDarkFont(true).init();
                        showFragment(2);
                        break;
                    case R.id.rb_my:
                        showFragment(3);
                        ImmersionBar.with(MainActivity.this).keyboardEnable(false).statusBarDarkFont(true, 0.2f).navigationBarColor(R.color.btn3).init();
                        break;
                }
            }
        });
    }

    // 按导航栏展示fragment
    public void showFragment(int i) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        hideFragments(ft);
        switch (i) {
            case 0:
                if (homeFragment != null)
                    ft.show(homeFragment);
                else {
                    homeFragment = new HomeFragment();
                    ft.add(R.id.frameLayout, homeFragment);
                }

                break;
            case 1:
                if (shareFragment != null)
                    ft.show(shareFragment);
                else {
                    shareFragment = new ShareFragment();
                    ft.add(R.id.frameLayout, shareFragment);
                }

                break;
            case 2:
                if (videoFragment != null)
                    ft.show(videoFragment);
                else {
                    videoFragment = new VideoFragment();
                    ft.add(R.id.frameLayout, videoFragment);
                }

                break;
            case 3:
                if (meFragment != null)
                    ft.show(meFragment);
                else {
                    meFragment = new MeFragment();
                    ft.add(R.id.frameLayout, meFragment);
                }
                // 沉浸式管理导航栏、状态栏
                ImmersionBar.with(this).keyboardEnable(false).statusBarDarkFont(false).navigationBarColor(R.color.colorPrimary).init();
                break;

        }
        // 提交上述fragment事务的逻辑处理，非常重要，否则上述代码将不会在UI界面上生效
        // 而且这个方法是异步的
        ft.commit();
    }

    // 隐藏没有显示在当前的fragment
    private void hideFragments(FragmentTransaction ft2) {
        if (homeFragment != null)
            ft2.hide(homeFragment);
        if (shareFragment != null)
            ft2.hide(shareFragment);
        if (videoFragment != null)
            ft2.hide(videoFragment);
        if (meFragment != null)
            ft2.hide(meFragment);

    }



}
